//
//  EmptyViewKitTool.m
//  AFNetworking
//
//  Created by 海参 on 2022/6/29.
//

#import "CAIImagePickerKitTool.h"

@implementation CAIImagePickerKitTool


+ (NSBundle *)bundle
{
    static NSBundle *bundle = nil;
    if (bundle == nil) {
        //.framework
        NSBundle *bundle1 = [NSBundle bundleForClass:[self class]];
        //.bundle
        NSString *path = [bundle1 pathForResource:@"CAIImagePickerControllerKit" ofType:@"bundle"];
        bundle = [NSBundle bundleWithPath:path];
    }
    return bundle;
}


+ (UIImage *)imageNamed:(NSString *)name
{
    NSBundle *bundle = [self bundle];
    UIImage *img = [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    return img;
}


+ (CGFloat)safeAreaStatusBarHeight
{
    CGFloat statusBarHeight = 0;
    if (@available(iOS 13.0, *)) {
        //待确认
//        NSSet <UIScene *>*connectedScenes = [UIApplication sharedApplication].connectedScenes;
//        UIWindowScene *scene = nil;
//        if (![UIApplication sharedApplication].supportsMultipleScenes) {
//            scene = (UIWindowScene *)[[connectedScenes allObjects] firstObject];
//        }else{
//            for (UIWindowScene * windowScene in connectedScenes) {
//                if (windowScene.activationState == UISceneActivationStateForegroundActive) {
//                    scene = windowScene;
//                }
//            }
//        }
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *scene = (UIWindowScene *)[[set allObjects] firstObject];
        statusBarHeight = scene.statusBarManager.statusBarFrame.size.height;
    } else
    {
        statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    }
    return statusBarHeight;
}




#pragma mark - 修正图片转向

/// 会耗时0.17s;必须主线程
/// 修正图片转向属性imageOrientation，其它不变；好奇怪，正常竖着拍是UIImageOrientationRight方向的，而横着拍反而是UIImageOrientationUp？
//转向前：<UIImage:0x283a2a760 anonymous {3024, 4032}> ,imageOrientation=3
//转向后：<UIImage:0x283a2a760 anonymous {3024, 4032}> ,imageOrientation=0
+ (UIImage *)fixOrientation:(UIImage *)aImage {
//    if (!self.shouldFixOrientation) return aImage;
   
//    // No-op if the orientation is already correct
//    if (aImage.imageOrientation == UIImageOrientationUp)
//        return aImage;
//
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}




+ (NSString *)localizedString2FromDate:(NSDate *)date
                            dateFormat:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    formatter.dateFormat = format;
    NSString *string = [formatter stringFromDate:date];
    return string;
}

@end



